package com.yhsoft.flim.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.FuSiLingFlimApiService;
import com.yhsoft.flim.controller.po.JsonData;
import com.yhsoft.flim.entity.HotMovie;
import com.yhsoft.flim.mapper.HotMovieMapper;
import com.yhsoft.flim.service.IHotMovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 热映影片信息 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-12-05
 */
@Service
@Slf4j
public class HotMovieServiceImpl extends ServiceImpl<HotMovieMapper, HotMovie> implements IHotMovieService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void synJob() throws IOException {
        String msg = FuSiLingFlimApiService.hostList();
        System.out.println("response >>> : " + msg);

        JsonData hotMovieData = JSON.parseObject(msg, JsonData.class);
        List<HotMovie> hotMovies = JSON.parseArray(hotMovieData.getData().toString(), HotMovie.class);
        hotMovies.forEach((hotMovie) -> {
            System.out.println(hotMovie.getMovieId());
            System.out.println(hotMovie.getName());
            System.out.println(hotMovie.getActorList());
            hotMovie.setUpdatedTime(LocalDateTime.now());
            hotMovie.setDeleted(0);
        });
        this.saveOrUpdateBatch(hotMovies);
    }

    @Override
    public List<HotMovie> getHotMovie(String keyStr) {
        // 根据key从redis中查询热映影片信息，查不到就调接口
        String key = keyStr;
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj != null) {
            // 解析
            List<HotMovie> hotMovieList = JSON.parseArray(obj.toString(), HotMovie.class);

            if (hotMovieList.size() > 0) {
                return hotMovieList;
            } else {
                return null;
            }
        }

        // 调福司令接口
        // 调福司令接口
        try {
            String msg = "";
            if (keyStr.equals("HotMovie")) {
                msg = FuSiLingFlimApiService.hostList();
            } else if (keyStr.equals("SoonMovie")) {
                msg = FuSiLingFlimApiService.soonList();
            }


            log.info(msg);

            JsonData hotMovieJson = JSON.parseObject(msg, JsonData.class);
            if (hotMovieJson.getData() == null || hotMovieJson.getData().size() == 0) {
                return null;
            }

            String data = hotMovieJson.getData().toString();

            // 存入redis
            redisTemplate.opsForValue().set(key, data, 12, TimeUnit.HOURS);

            List<HotMovie> hotMovieList = JSON.parseArray(data, HotMovie.class);

            return hotMovieList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
